package com.kele.controller;

import com.kele.domain.Collections;
import com.kele.domain.Follow;
import com.kele.domain.User;
import com.kele.service.FollowService;
import com.kele.service.UserService;
import com.kele.utils.ResponseData;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/follow")
public class FollowController {
    @Resource
    private FollowService followService;

    @Resource
    private UserService userService;

    @RequestMapping(value="/Follow",method = RequestMethod.GET)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id",name = "userid",required = true,type = "int"),
            @ApiImplicitParam(value = "被关注的用户id",name = "bloggerid",required = true,type = "int")
    })
    public ResponseData Follow(int userid,int bloggerid){
        Follow fl=followService.findFollow(userid,bloggerid);
        if(fl!=null){//如果查询记录不为空,那么就说明用户有关注操作
            //关注-1,粉丝-1
            //进行删除关注记录
            int count=followService.deleteFollow(fl.getFollowid());
            System.out.println("删除成功,删除的行数是"+count);
            //查找被关注的用户,使其粉丝-1
            User user=userService.queryUserById(bloggerid);
            userService.subFans(user.getId());
            //查找执行关注的用户,使其关注-1
            User user1=userService.queryUserById(userid);
            userService.subFollow(user1.getId());
            return ResponseData.success("取消关注成功,用户关注数-1,被关注者粉丝数-1",user.getFans()-1);
        }else{
            //先查询被关注的用户,使其粉丝+1
            User user=userService.queryUserById(bloggerid);
            userService.addFans(user.getId());
            //在查找执行关注的用户,使其关注+1
            User user1=userService.queryUserById(userid);
            userService.addFollow(user1.getId());
            //添加关注记录
            Follow follow=new Follow();
            follow.setUserid(userid);
            follow.setBloggerid(bloggerid);
            follow.setBlogger(user.getNickname());
            Integer integer=followService.insertFollow(follow);
            System.out.println("添加关注记录记录"+integer);
            return  ResponseData.success("关注成功",user.getFans()+1);
        }

    }

    @RequestMapping(value = "/findFansList",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData findFansList(int bloggerid){
        List<Follow> followList=followService.findFansList(bloggerid);
        if(followList.size()!=0)
        {
            return ResponseData.success("粉丝列表查询成功",followList);
        }else{
            return  ResponseData.fail("您还唯有粉丝哦");
        }
    }

    @RequestMapping(value = "/findFollowList",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData findFollowList(int userid){
        List<Follow> followList=followService.findFollowList(userid);
        if(followList.size()!=0){
            return ResponseData.success("关注列表查询成功",followList);
        }else{
            return ResponseData.fail("关注列表查询失败");
        }
    }




}
